#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\methods\montecarlo\path.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Methods { namespace Montecarlo {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IPath
	public ref class CPath  : 
            public Cephei::QL::Methods::Montecarlo::IPath
	{
	protected: 
		boost::shared_ptr<QuantLib::Path>* _ppPath;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Path>* _phPath;
#endif
		Object^ _PathOwner;     // reference to object that manages the storage for this object
	internal:
		CPath (Cephei::QL::ITimeGrid^ timeGrid, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ values);
        CPath (boost::shared_ptr<QuantLib::Path>& childNative, Object^ owner);
        CPath (QuantLib::Path& childNative, Object^ owner);
        CPath (CPath^ copy);
        CPath (PLATFORM::Type^ t);
#ifdef STRUCT
        CPath (QuantLib::Path childNative);
#endif       
#ifdef HANDLE
		CPath (QuantLib::Handle<QuantLib::Path>& childNative, Object^ owner);
		CPath (QuantLib::Handle<QuantLib::Path> childNative);
#endif
		virtual ~CPath ();
		!CPath ();

	internal:
		QuantLib::Path& GetReference ();
		boost::shared_ptr<QuantLib::Path>& GetShared ();
		QuantLib::Path* GetPointer ();
        void SetPath (boost::shared_ptr<QuantLib::Path> native)
        {
            if (_ppPath != NULL)
                delete _ppPath;
            _ppPath = new boost::shared_ptr<QuantLib::Path> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Path>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Double At (UInt64 i) ;
        property Double Back 
        {
		    virtual Double get () ;
        }
        property Boolean Empty 
        {
		    virtual Boolean get () ;
        }
        property Double Front 
        {
		    virtual Double get () ;
        }
        property UInt64 Length 
        {
		    virtual UInt64 get () ;
        }
		virtual Double Time (UInt64 i) ;
        property Cephei::QL::ITimeGrid^ TimeGrid 
        {
		    virtual Cephei::QL::ITimeGrid^ get () ;
        }
		virtual Double Value (UInt64 i) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IPath Vector interface
	ref class CPathVector sealed : 
							    public Core::INativeVector<IPath^>,
							    public Core::INativeVector<Core::Generic::ICoCell<IPath^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::Path>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::Path>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Path>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CPathVector ();
#ifdef STRUCT
		CPathVector (std::vector<QuantLib::Path>& vec, Object^ owner);
		CPathVector (std::vector<QuantLib::Path> vec);
#endif       
		CPathVector (std::vector<boost::shared_ptr<QuantLib::Path>>& vec, Object^ owner);
#ifdef HANDLE
		CPathVector (std::vector<QuantLib::Handle<QuantLib::Path>>& vec, Object^ owner);
#endif
		virtual ~CPathVector ();
		!CPathVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IPath^ default[int] 
		{
			virtual IPath^ get (int index) = Core::INativeVector<IPath^>::default::get;
			virtual void set (int index, IPath^ value) = Core::INativeVector<IPath^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::IPath^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<IPath^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<IPath^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<IPath^>^ value) = Core::INativeVector<Core::Generic::ICoCell<IPath^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::Path>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::Path>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Path>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::IPath^>::typeid)]
	[FactoryFor(Cephei::QL::Methods::Montecarlo::IPath::typeid)]
	[FactoryFor(Cephei::QL::Methods::Montecarlo::IPath_Factory::typeid)]
	public ref class CPath_Factory sealed : public IPath_Factory
	{
	public:
        virtual IPath^ Create (Cephei::QL::ITimeGrid^ timeGrid, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ values);
        /* <summary> Create a hybrid Vector of IPath, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<IPath^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IPath, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IPath^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<IPath^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<IPath^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IPath^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<IPath^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Methods::Montecarlo::IPath^>^ CreateNativeVector ()
		{
			return REFNEW CPathVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Methods::Montecarlo::IPath^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Methods::Montecarlo::IPath^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Methods*/ } /*Montecarlo */}
